package com.sidianzhong.sdz.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.sidianzhong.sdz.model.commond.SubmitBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

@Configuration
@Component
public class SocketServer {


    public static List<SubmitBean> list = new ArrayList<>();
    public static boolean isOpen;  //是否开启


    private  static Integer port;
    @Value(value = "${socketServer.port}")
    public void setPort(Integer ports){
        port = ports;
    }
    public static void startAction() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);  //端口号
            System.out.println("服务端服务启动监听");
            while (true) {
                Socket socket = serverSocket.accept();
                new Thread(new MyRuns(socket)).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    static class MyRuns implements Runnable {

        private Socket socket;
        private InputStream inputStream;

        MyRuns(Socket socket) {
            super();
            this.socket = socket;
        }

        public void run() {
            try {
                //读取客户端消息
                inputStream = socket.getInputStream();

                byte[] read = new byte[1024];
                int size;

                while ((size = inputStream.read(read, 0, read.length)) != -1) {
                    String s2 = new String(read, 0, size);
                    String str = new String(s2.getBytes("utf-8"), "utf-8");
                    String str1 = str.substring(0, str.indexOf("{"));
                    String str2 = str.substring(str1.length() + 1, str.length());
                    String s = "{" + str2;
                    System.out.print(s);

                    JSONObject jsonObject = JSONObject.parseObject(s);
                    Integer operatingState = jsonObject.getInteger("operatingState");
                    JSONArray returnMessage = jsonObject.getJSONArray("returnMessage");
                    if (operatingState != 0 && SocketServer.isOpen) {
                        SubmitBean submitBean = new SubmitBean();
                        submitBean.setTime(null);
                        submitBean.setName("打卡机失效");
                        list.add(submitBean);
                    }
                    if (returnMessage.get(0) != null && operatingState == 0) {

                        returnMessage.stream().forEach(ss -> {
                            String s1 = JSONObject.toJSONString(ss);
                            JSONObject jsonObject1 = JSONObject.parseObject(s1);
                            JSONArray returnParam = jsonObject1.getJSONArray("ReturnParam");
                            String name = returnParam.getString(0);
                            String time = returnParam.getString(1);

                            boolean b = true;
                            for (int i = 0; i < SocketServer.list.size(); i++) {
                                if (SocketServer.list.get(i).getName().equals(name) && SocketServer.list.get(i).getTime().equals(time)) {
                                    b = false;
                                    break;
                                }
                            }
                            if (b) {
                                SubmitBean submitBean = new SubmitBean();
                                submitBean.setName(name);
                                submitBean.setTime(time);
                                list.add(submitBean);
                            }
                            System.out.println(returnMessage);
                        });
                    }
                }
                System.out.println("服务端监听关闭");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    if (socket != null) {
                        socket.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
}